import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:ride_rex/main/mine/setting/setting_logic.dart';
import 'package:ride_rex/service/user_service.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../../standard/app_colors.dart';

class SettingPage extends StatelessWidget {
  SettingPage({super.key});

  final SettingLogic logic = SettingLogic()..init();

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: 'Setting',
      scaffoldBackgroundColor: Colors.white,
      child: ValueListenableBuilder(
        valueListenable: UserService.instance.notifierOfMineInfo,
        builder: (BuildContext context, value, Widget? child) {
          return Column(
            children: [
              _buildItem(
                'ID：${UserService.instance.notifierOfMineInfo.value.userId ?? ''}',
                right: ButtonWidget.text(
                  'copy',
                  textColor: AppColors.primary,
                  fontSize: 10,
                  backgroundColor: Colors.white,
                  alignment: Alignment.center,
                  width: 35.w,
                  height: 15.w,
                  borderRadius: 8.w,
                  onTap: () => CommonOperate.copy(
                    UserService.instance.notifierOfMineInfo.value.userId ?? '',
                    toast: 'Copy success',
                  ),
                ),
              ).padding(top: 8.w),
              _buildItem(
                'Auto Translate',
                onTap: () {},
                right: ValueListenableBuilder(
                  valueListenable: logic.autoTranslate,
                  builder: (BuildContext context, bool value, Widget? child) {
                    return FlutterSwitch(
                      width: 46.w,
                      height: 26.w,
                      value: value,
                      padding: 3.w,
                      activeColor: AppColors.primary,
                      onToggle: (val) {
                        logic.onTaoAutoTranslate();
                      },
                    );
                  },
                ),
              ).padding(top: 16.w),
              _buildItem(
                'Logout',
                onTap: logic.logout,
              ).padding(top: 16.w),
              _buildItem(
                'Delete Account',
                onTap: logic.deleteAccount,
              ).padding(top: 16.w),
            ],
          ).paddingHorizontal(16.w);
        },
      ),
    );
  }

  Widget _buildItem(String title, {VoidCallback? onTap, Widget? right}) {
    return ButtonWidget(
      onTap: onTap,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.all(14.w),
      backgroundColor: '#F6F8FA'.toColor,
      borderRadius: 8.w,
      hasInkWell: onTap != null,
      child: Row(
        children: [
          TextWidget.oneLine(text: title, fontSize: 16, color: AppColors.text).expanded(),
          if (right != null) right,
        ],
      ),
    );
  }
}
